﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;

namespace TireService.Web.UserControls
{
    public partial class ucFindAvailableServiceCarScheduleDetail : System.Web.UI.UserControl
    {
        private string _controlname = "FindCapacity";

        //public int TaskId
        //{
        //    get
        //    {
        //        if (ViewState["_taskid"] != null)
        //            return (int)ViewState["_taskid"];
        //        else
        //            return 0;
        //    }
        //    set
        //    {
        //        ViewState["_taskid"] = value;
        //    }
        //}

        //public Boolean UserControlLoaded
        //{
        //    get
        //    {
        //        if (ViewState["_availablecapacityloaded"] != null)
        //            return (Boolean)ViewState["_availablecapacityloaded"];
        //        else
        //            return false;
        //    }
        //    set
        //    {
        //        ViewState["_availablecapacityloaded"] = value;
        //    }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(Session["UserControlLoaded" + _controlname]))
            {
                // Initiate datalayer
                TireServiceDB db = new TireServiceDB();

                //LoadAvailableCapacity(this.TaskId, db);
                LoadAvailableCapacity(Convert.ToInt32(Session["TaskId" + _controlname]), db);
            }
        }

        protected void callbackFindScheduleDetailPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int TaskId)
        {
            //this.TaskId = TaskId;
            Session["TaskId" + _controlname] = TaskId;

            // Show usercontrol edit form
            pFindScheduleDetail.Visible = true;

            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            if (TaskId != 0)
            {
                // TODO:
                LoadAvailableCapacity(TaskId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Service kørsel blev ikke fundet");
            }
        }

        private void LoadAvailableCapacity(int TaskId, TireServiceDB db)
        {
            Task task = db.GetTask(TaskId);
            
            var details = from p in db.ServiceCarScheduleDetails
                          where p.WorkCompleted == false & p.ScheduleDate >= DateTime.Now & p.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.ServiceDrive
                          select new
                          {
                              p.ServiceCarScheduleDetailId,
                              p.ScheduleDate,
                              p.ServiceCarId,
                              p.ToAddress,
                              p.ToZipCode,
                              p.ToCity,
                              p.ToLatitude,
                              p.ToLongitude,
                              FreeCapacity = db.ServiceCarScheduleDetails.Where(d => d.ServiceCarId == p.ServiceCarId & d.ScheduleDate == p.ScheduleDate & d.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.Available).Count(),
                              Distance = db.DistanceBetween((float)task.PrimaryLatitude, (float)task.PrimaryLongitude, (float)p.ToLatitude, (float)p.ToLongitude),
                              DistanceSecondary = (task.SecondaryLatitude == null & task.SecondaryLongitude == null ? 0 : db.DistanceBetween((float)task.SecondaryLatitude, (float)task.SecondaryLongitude, (float)p.ToLatitude, (float)p.ToLongitude))
                          };

            //ServiceCarAreaAllocationId = (z.ServiceCarAreaAllocationId == null ? 0 : z.ServiceCarAreaAllocationId),

            if (details != null)
            {
                gvAvailableCapacity.DataSource = details.Where(p => p.FreeCapacity > 0).ToList();
                gvAvailableCapacity.DataBind();
            }

            Session["UserControlLoaded" + _controlname] = true;
        }
    }
}